#!/usr/bin/evn python
# -*- coding: UTF-8 -*-
# @File: app.py
# @Author: fy
# @date: 2021/9/13 9:58
# 手机启动相关操作
from appium import webdriver
from pages.base_page import BasePage
from pages.start_page import StartPage
from time import sleep

class App(BasePage):
    def start(self, command_executor: str = "http://127.0.0.1:4723/wd/hub",
              desired_capabilities: dict = None):
        '''
        启动app
        :return:
        '''
        if self.driver is None:
            desired_cap = {}
            if desired_capabilities:
                desired_cap = desired_capabilities
            if desired_cap.get("platformName", "") == "":
                desired_cap["platformName"] = "Android"

            # if desired_cap.get("automationName", "") == "":
            #     desired_cap["automationName"] = "UiAutomator2"
            if desired_cap.get("deviceName", "") == "":
                desired_cap["deviceName"] = "127.0.0.1:62001"
            if desired_cap["platformName"] == "Android":
                if desired_cap.get("appActivity", "") == "":
                    desired_cap["appActivity"] = "com.tencent.wework.launch.LaunchSplashActivity"
                if desired_cap.get("appPackage", "") == "":
                    desired_cap["appPackage"] = "com.tencent.wework"
                if desired_cap.get("skipUnlock", "") == "":
                    desired_cap["skipUnlock"] = "True"
                if desired_cap.get("unicodeKeyboard", "") == "":
                    desired_cap["unicodeKeyboard"] = "True"
                if desired_cap.get("resetKeyboard", "") == "":
                    desired_cap["resetKeyboard"] = "True"
                if desired_cap.get("dontStopAppOnReset", "") == "":
                    desired_cap["dontStopAppOnReset"] = "True"
                if desired_cap.get("adbExecTimeout", "") == "":
                    desired_cap["adbExecTimeout"] = "120000"
                # if desired_cap["chromedriverExecutableDir"] is None:
                #     desired_cap["chromedriverExecutableDir"] = r"D:\androidApk"
                # if desired_cap["chromedriverChromeMappingFile"] is None:
                #     desired_cap["chromedriverChromeMappingFile"] =

            if desired_cap.get("noReset", "") == "":
                desired_cap["noReset"] = "True"

            self.driver = webdriver.Remote(command_executor=command_executor, desired_capabilities=desired_cap)
            DRIVER_APP_GLOBAL = self.driver
            pass
        else:
            self.driver.launch_app()
            DRIVER_APP_GLOBAL = self.driver

    def close(self):
        self.driver.quit()
        pass

    def goto_startPage(self):
        return StartPage(self.driver)

    def back_Page(self, times):
        '''

        :param times: 回退的次数
        :return:
        '''
        time = 0
        while time < times:
            self.back()
            sleep(1)
            time += 1
            print(time)
